package com.lfd.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lfd.vo.OptionVo;
import com.lfd.vo.ProductVo;
import com.lfd.entity.Product;
import com.lfd.service.ProductService;
import com.lfd.util.Result;

@Controller
@RequestMapping("/products")
public class ProductController {
	private Logger logger = Logger.getLogger(ProductController.class);
	@Autowired
	private ProductService productService;



	private ProductVo selectVo;

	/**
	 * 查询所有
	 * 
	 * @param product
	 * @param page
	 * @param limit
	 * @param requedt
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/list")
	public Result list(Product product, @RequestParam(name = "page", defaultValue = "1") Integer page,
			@RequestParam(name = "limit", defaultValue = "10") Integer limit, HttpServletRequest requedt) {
		String str = requedt.getRealPath("/");
		List<ProductVo> queryList = productService.queryList(product, page, limit);
		int countAll = productService.CountAll();
		return new Result("0", "成功", countAll, queryList);

	}

	/**
	 * 添加
	 * 
	 * @param product
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addOne")
	public Result addOne(Product product) {
		try {
			productService.addOne(product);
		} catch (Exception e) {
			logger.error("添加失败", e);
			return new Result("1", "添加失败");
		}
		return new Result("0", "添加成功");

	}

	/**
	 * 下拉框
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/selsctOpResults")
	public Result selsctOpResult() {
		List<OptionVo> selectOptions = new ArrayList();
		try {
			selectOptions = productService.selectOptions();
		} catch (Exception e) {
			logger.error("选择下拉框失败", e);
			return new Result("1", "失败", selectOptions.size(), selectOptions);
		}
		return new Result("0", "成功", selectOptions.size(), selectOptions);

	}

	/**
	 * 批量删除
	 * 
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/batchDel")
	public Result delAll(@RequestParam("ids") List<Long> ids) {
		productService.batchDel(ids);
		return new Result("0", "成功", 100, null);
	}

	/**
	 * 查询
	 * 
	 * @param pid
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/query")
	public Result selectbyId(@RequestParam("pid") Long pid) {
		ProductVo selectVo = productService.selectVo(pid);
		selectVo.setPid(pid);
		return new Result("0", "查询成功", 1, selectVo);
	}

	/**
	 * 修改
	 * 
	 * @param product
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/update")
	public Result updateResult(Product product) {
		try {
			productService.updateByid(product);
		} catch (Exception e) {
			logger.error("修改失败", e);
			return new Result("1", "失败");
		}
		return new Result("0", "成功");

	}

	/**
	 * 单个删除
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Result deleteResult(Long id) {
		productService.deleteById(id);
		return new Result("0", "成功", 100, null);
	}

	/**
	 * 批量下架
	 * 
	 * @param downs
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/batchdown")
	public Result down(@RequestParam("ids") List<Integer> ids) {
		productService.batchDown(ids);
		return new Result("0", "下架成功", 100, null);
	}

	/**
	 * 批量上架
	 * 
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/batchup")
	public Result on(@RequestParam("ids") List<Integer> ids) {
		productService.batchOn(ids);
		return new Result("0", "上架成功", 100, null);

	}
	
	/**
	 * 推荐商品
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/ptj")
	public Result tj() {
		List<ProductVo> Ptj = new ArrayList();
		try {
				Ptj=  productService.Ptj();
		} catch (Exception e) {
			logger.error("查询失败", e);
			return new Result("1", "失败",Ptj.size(),Ptj);
		}
		return new Result("0", "成功",Ptj.size(),Ptj);
		
	}
	
	
	/**
	 * 热门商品
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/rx")
	public Result rx() {
		List<ProductVo> rx = new ArrayList();
		try {
			rx=  productService.rx();
		} catch (Exception e) {
			logger.error("查询失败", e);
			return new Result("1", "失败",rx.size(),rx);
		}
		return new Result("0", "成功",rx.size(),rx);
		
	}
	
	/**
	 * 轮播图
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/lpt")
	public Result lpt() {
		List<ProductVo> lpt = new ArrayList();
		try {
			lpt=  productService.lpt();
		} catch (Exception e) {
			logger.error("查询失败", e);
			return new Result("1", "失败",lpt.size(),lpt);
		}
		return new Result("0", "成功",lpt.size(),lpt);
		
	}
	
	
}
